package com.test.controller;

import com.topsoft.common.RedisService;
import com.topsoft.common.BaseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping()
public class ClientController {

    @Autowired
    RedisService redisService;


    @Autowired
    @Qualifier("redisTemplateMessage")
    RedisTemplate redisTemplate;

//    @GetMapping("/send/{queue}/{data}")
//    public Object send(@PathVariable("queue") String queue, @PathVariable("data") String data){
//        BaseMessage baseMessage = new BaseMessage(AppContext.getKeyId(),data).recvQueue(queue);
//        messageTemplate.sendMessage(baseMessage);
//        return "发送成功";
//    }

//    @PostMapping("/send/{queue}/{msgId}")
//    public Object post(@PathVariable("queue") String queue, @PathVariable("msgId") String msgId, @RequestBody Object data){
//        BaseMessage baseMessage = new BaseMessage(msgId,data).recvQueue(queue).msgId(msgId);
//        messageTemplate.sendMessage(baseMessage);
//        return "发送成功";
//    }

    @Autowired Jackson2JsonRedisSerializer jackson2JsonRedisSerializer=null;

    @PostMapping("/send/{queue}")
    public Object post(@PathVariable("queue") String queue, @RequestBody List<BaseMessage> list){
        redisService.setRedis(queue, list);
        return "保存消息到Redis";
    }


    @GetMapping("/get")
    public Object get(){
//        String ls_keys = redisTemplate.opsForHash().entries()));


//        Cursor<Map.Entry<Object,Object>> cursor = redisTemplate
//            .opsForHash().scan(STORE_CITY_INFO, ScanOptions.scanOptions().count(1000).match("匹配规则").build());
//    while (cursor.hasNext()){
//        Map.Entry<Object,Object> entry = cursor.next();
//        System.out.println("通过scan(H key, ScanOptions options)方法获取匹配键值对:" + entry.getKey() + "---->" + entry.getValue());
//    }


        List list = new ArrayList();
        list.add("A1");
        list.add("2");list.add("A2");
        return redisService.batchGet("test","toh",list);
    }
}
